package com.ohuang.crashhandler;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class CrashLogActivity extends Activity {
    private TextView textView;
    private String string;
    private String fileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crash_log);
        textView = findViewById(R.id.tv_log);
        Intent intent = getIntent();
        fileName = intent.getStringExtra("filename");

        new Thread(runnable).start();
    }


    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                String path = getFilesDir().getAbsolutePath() + "/crashLog/"+fileName;
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                BufferedReader reader = null;
                StringBuffer sbf = new StringBuffer();
                try {
                    reader = new BufferedReader(new FileReader(dir));
                    String tempStr;

                    while ((tempStr = reader.readLine()) != null) {
                        sbf.append(tempStr);
                        if (sbf.length()>1000000){//避免数据太大
                            break;
                        }
                    }
                    reader.close();
                    string = sbf.toString();
                    textView.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(string);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }

            }
        }
    };
}